package cn.abcsys.cloud.devops.web.service.impl;

import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.abcsys.cloud.devops.web.dao.ClusterEnvMapper;
import cn.abcsys.cloud.devops.web.entity.ClusterEnv;
import cn.abcsys.cloud.devops.web.service.ClusterEnvService;
import cn.abcsys.cloud.devops.web.util.Result;

/**
 * 集群业务处理
 *
 */
@Component
public class ClusterEnvServiceImpl implements ClusterEnvService {
	private static final Logger LOGGER = Logger.getLogger(ClusterEnvServiceImpl.class);
	@Autowired
	private ClusterEnvMapper clusterEnvMapper;
	
	@Override
	@Transactional
	public Result createCLusterEnv(ClusterEnv clusterEnv) {
		Result result = new Result(true, "创建集群环境成功");
		try {
			//一个环境只有一个集群，所以创建前先删除该环境所在其他的集群
			clusterEnvMapper.deleteByEnv(clusterEnv.getEnvId());
			int issuccess = clusterEnvMapper.insertEnv(clusterEnv);
			if(issuccess!=1){
				result = new Result(false, "创建集群环境失败,原因未知");
			}
		} catch (SQLException e) {
			LOGGER.error("创建集群环境失败:"+e.getMessage());
			result = new Result(false, "创建集群环境失败："+e.getMessage());
		}
		LOGGER.info("创建成功");
		return result;
	}
	@Override
	public Result remove(Integer clusterId,Integer envId) {
		Result result = new Result(true, "删除集群环境成功");
		try {
			if(null!=clusterId){
				int issuccess = clusterEnvMapper.deleteByCluster(clusterId);
				if(issuccess!=1){
					result = new Result(false, "删除集群环境失败,原因未知");
				}
			}
			if(null!=envId){
				int issuccess = clusterEnvMapper.deleteByEnv(envId);
				if(issuccess!=1){
					result = new Result(false, "删除集群环境失败,原因未知");
				}
			}
		} catch (SQLException e) {
			LOGGER.error("删除集群环境失败:"+e.getMessage());
			result = new Result(false, "删除集群环境失败："+e.getMessage());
		}
		LOGGER.info("删除成功");
		return result;
	}
	
	@Override
	public Result getrClusterByEnvId(Integer envId) {
		// TODO Auto-generated method stub
		Result result = new Result(true, "查询环境对应集群信息");
		try {
			if(null!=envId){
				ClusterEnv clusterEnv = clusterEnvMapper.getClusterByEnv(envId);
				result.setData(clusterEnv);
			}
		} catch (SQLException e) {
			LOGGER.error("查询集群信息失败:"+e.getMessage());
			result = new Result(false, "查询集群信息失败："+e.getMessage());
		}
		return result;
	}
	
	
	/*@Override
	public Result getEnvByCluster(Integer clusterId) {
		Result result = null;
		try {
			List<Env> envs = envMapper.selectEnvListByClusterId(clusterId);
			if(null!=envs){
				result = new Result(true,JSONArray.toJSON(envs),"success");
			}else{
				result = new Result(false,"未查到相关结果");
			}
		} catch (SQLException e) {
			LOGGER.error("查询失败:"+e.getMessage());
			result = new Result(false, "查询失败："+e.getMessage());
		}
		LOGGER.info("创建成功");
		return result;
	}*/
	/*@Override
	public Result getClusterByEnv(Integer envId) {
		Result result = null;
		try {
			List<Cluster> clusters = clusterMapper.selectClusterListByEnvId(envId);
			if(null!=clusters){
				result = new Result(true,JSONArray.toJSON(clusters),"success");
			}else{
				result = new Result(false,"未查到相关结果");
			}
		} catch (SQLException e) {
			LOGGER.error("查询失败:"+e.getMessage());
			result = new Result(false, "查询失败："+e.getMessage());
		}
		LOGGER.info("创建成功");
		return result;
	}*/

}
